package com.wwb.w0503;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity3 extends AppCompatActivity {
    ListView listView;
    String[] messages=new String[]{"老师，早上好","老师，快放假了","老师，您辛苦了"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        listView=(ListView) findViewById(R.id.lv2);

        MyBaseAdapter mAdapter = new MyBaseAdapter();
        listView.setAdapter(mAdapter);
       // listView.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item2,messages));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent data=new Intent();
                data.putExtra("message",messages[i]);
                System.out.println(messages[i]);
                setResult(21,data);
                finish();
            }
        });
    }
    class MyBaseAdapter extends BaseAdapter {

        @Override
        public int getCount() {

            return messages.length;
        }

        @Override
        public Object getItem(int i) {

            return messages[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1=View.inflate(MainActivity3.this,R.layout.list_item2,null);

            TextView title=view1.findViewById(R.id.messagelist);

            title.setText(messages[i]);

            return view1;
        }
    }
}